import 'package:flame/components.dart';
import 'package:flame/events.dart';
import '../../../../config/GameConfig.dart';
import '../../../../routes/RouteGame.dart';
import '../../../../config/AssetsName.dart';

// 种子商店背景
class ShopBackground extends PositionComponent
    with TapCallbacks, HasGameReference<RouterGame>, GameConfig {
  ShopBackground();

  late final Sprite shopSprite;

  @override
  Future<void> onLoad() async {
    shopSprite = await Sprite.load(AssetsName.plantMenuShop);

    final originalWidth = shopSprite.image.width.toDouble();
    final originalHeight = shopSprite.image.height.toDouble();
    // 目标宽度（拉伸后的总宽度）
    final originalStretchWidth = originalWidth - menuFixedLeft - menuFixedRight;
    final partLeftWidth = menuFixedLeft / originalHeight * menuHeight;
    final partRightWidth = menuFixedRight / originalHeight * menuHeight;
    final targetStretchWidth = menuTargetWidth - partLeftWidth - partRightWidth;
    // 将植物商店分为三部分
    add(
      SpriteComponent(
        sprite: Sprite(
          shopSprite.image,
          srcPosition: Vector2(0, 0),
          srcSize: Vector2(menuFixedLeft, originalHeight),
        ),
        position: Vector2(menuFixedLeftOffset, 0),
        size: Vector2(partLeftWidth, menuHeight),
      ),
    );
    add(
      SpriteComponent(
        sprite: Sprite(
          shopSprite.image,
          srcPosition: Vector2(menuFixedLeft, 0),
          srcSize: Vector2(originalStretchWidth, originalHeight),
        ),
        position: Vector2(partLeftWidth + menuFixedLeftOffset, 0),
        size: Vector2(targetStretchWidth, menuHeight),
      ),
    );
    add(
      SpriteComponent(
        sprite: Sprite(
          shopSprite.image,
          srcPosition: Vector2(menuFixedLeft + originalStretchWidth, 0),
          srcSize: Vector2(menuFixedRight, originalHeight),
        ),
        position: Vector2(
          partLeftWidth + targetStretchWidth + menuFixedLeftOffset,
          0,
        ),
        size: Vector2(partRightWidth, menuHeight),
      ),
    );
    return super.onLoad();
  }
}
